const { ccclass, property, menu, playOnFocus ,inspector, executeInEditMode } = cc._decorator;

@ccclass
@menu('自定义组件/skeleton-preview')
@executeInEditMode
@playOnFocus
@inspector("packages://easygame/inspector/other/skeletonPreview.js")

export default class SkePreview extends cc.Component {
    @property({type:sp.Skeleton})
    skeleton:sp.Skeleton = null

    @property
    animations = []

    @property
    _isLoop:boolean = true

    @property
    set isLoop(value){
        this._isLoop = value
        this.skeleton.setAnimation(0,this._play,this._isLoop)
    }
    get isLoop(){
        return this._isLoop
    }

    @property
    _play:string = ""

    @property
    set play(value){
        this._play = value
        this.skeleton.setAnimation(0,this._play,this._isLoop)
    }
    get play(){
        return this._play
    }

    @property()
    _stopPreview: string = ""

    @property
    set stopPreview(value) {
        console.log(value)
        if(value == "-1"){
            this.skeleton.setAnimation(0,this.skeleton.animation,this.isLoop)
            this.skeleton.paused = true
        }else{
            this.skeleton.setAnimation(0,this.skeleton.animation,this.isLoop)
            this.skeleton.paused = false
        }
    }
    get stopPreview() {
        return this._stopPreview
    }

    onLoad() {
        if (CC_DEBUG) {
            if(!this.skeleton){
                this.skeleton = this.getComponent(sp.Skeleton)
                let ani = this.skeleton["_skeleton"]["data"]["animations"]
                this.skeleton.setAnimation(0,this.skeleton.animation,this.isLoop)
                for (let index = 0; index < ani.length; index++) {
                    this.animations.push(ani[index].name)
                }
            }

            sp.Skeleton.prototype['update'] = function (dt) {
                if (CC_EDITOR) {
                    cc['engine']._animatingInEditMode = 1;
                    cc['engine'].animatingInEditMode = 1;
                }
                if (this.paused) return;
        
                dt *= this.timeScale * sp['timeScale'];
        
                if (this.isAnimationCached()) {
                    if (this._isAniComplete) {
                        if (this._animationQueue.length === 0 && !this._headAniInfo) {
                            let frameCache = this._frameCache;
                            if (frameCache && frameCache.isInvalid()) {
                                frameCache.updateToFrame();
                                let frames = frameCache.frames;
                                this._curFrame = frames[frames.length - 1];
                            }
                            return;
                        }
                        if (!this._headAniInfo) {
                            this._headAniInfo = this._animationQueue.shift();
                        }
                        this._accTime += dt;
                        if (this._accTime > this._headAniInfo.delay) {
                            let aniInfo = this._headAniInfo;
                            this._headAniInfo = null;
                            this.setAnimation(0, aniInfo.animationName, aniInfo.loop);
                        }
                        return;
                    }
        
                    this._updateCache(dt);
                } else {
                    this._updateRealtime(dt);
                }
            }
        }
    }
}
